package com.doodod.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: ydw
 * Date: 15-4-16
 * Time: 上午10:12
 */
public class DateUtil extends Date{

    /** 年月日时分秒(无下划线) yyyyMMddHHmmss */
    public static final String dtLong                  = "yyyyMMddHHmmss";

    /** 完整时间 yyyy-MM-dd HH:mm:ss */
    public static final String simple                  = "yyyy-MM-dd HH:mm:ss";

    /** 年月日(无下划线) yyyyMMdd */
    public static final String dtShort                 = "yyyyMMdd";

    /** 年月日(无下划线) yyyyMMdd */
    public static final String dtShort_                = "yyyy-MM-dd";


    /**
     * 返回系统当前时间(精确到毫秒),
     * @return
     */
    public  static String getTime(String format){
        Date date=new Date();
        DateFormat df=new SimpleDateFormat(format);
        return df.format(date);
    }

    /**
     * 得到当前时间间隔N天的Date对象
     * @param N
     * @return
     */
    public static Date getDate(int N){
        Date date = new Date();
        Calendar calendar  = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, N);//把日期往后增加一天.整数往后推,负数往前移动
        date=calendar.getTime();   //这个时间就是日期往后推一天的结果
        return date;
    }

    /**
     * 产生随机的三位数
     * @return
     */
    public static String getThree(){
        Random rad=new Random();
        return rad.nextInt(1000)+"";
    }

    /**
     * 获取今天0点的unix时间戳
     *
     * @return
     */
    public static long getTodayZeroUnixTime() {
        SimpleDateFormat shortDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String tody = shortDateFormat.format(date);
        Date date1 = null;
        try {
            date1 = shortDateFormat.parse(tody);
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return date1.getTime() / 1000;
    }

    /**
     * 按照指定格式返回当前时间
     *
     * @param format
     * @return
     */
    public static String getCurrentTimeString(String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(new Date());
    }

    /**
     * 获取距离指定时间指定格式的时间
     *
     * @param format
     * @param current
     * @param n
     * @return
     */
    public static String getNDaysGap(String format, String current, int n) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = simpleDateFormat.parse(current);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, n);
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取前N天的日期
     * @param format
     * @param n
     * @return
     */
    public static String getPastDay(String format, int n){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -n);
        String date = new SimpleDateFormat(format).format(cal.getTime());
        return date;
    }


    public static long getUnixTimeByString(String format, String date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date1 = null;
        try {
            date1 = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date1.getTime() / 1000;

    }

    /**
     * 两个日期间隔日期
     * * @param
     * @param
     * @return int
     * @throws ParseException
     */
    public static int getDateSpace(String early, String late)
            throws ParseException {


        Date earlydate = new Date();
        Date latedate = new Date();
        DateFormat df = DateFormat.getDateInstance();

        try {
            earlydate = df.parse(early);
            latedate = df.parse(late);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        //得到两个日期相差的天数
        int days = ((int) (latedate.getTime() / 1000) - (int) (earlydate.getTime() / 1000)) / 3600 / 24;

        return days;
    }


    public static void main(String args[]) {

        try {
            int days = DateUtil.getDateSpace("2015-04-29","2015-05-03");
            System.out.print(days);
        } catch (ParseException e) {
            e.printStackTrace();
        }


    }

}
